const app = getApp()

// 截取二维码参数
function getParams(url, params) {
    var res = new RegExp("(?:&|/?)" + params + "=([^&$]+)").exec(url);
    return res ? res[1] : '';
}

//
function getCarStore(type) {
    wx.showLoading()
    return new Promise((resolve, reject) => {
        let params = {

        }
        app.$http.get('/hd_project/Play/getCarStore', params).then(res => {
            wx.hideLoading()
            if (res.code == 200) {
                resolve(res)
            } else {
                const error = new Error("数据获取失败");
                wx.showToast({
                    title: res.message,
                    icon: "none",
                    duration: "3000"
                })
                  reject(res)
            }
        })
    })
}


// 获取打卡任务列表
function getProjectList() {
    wx.showLoading()
    return new Promise((resolve, reject) => {
        app.$http.get('/hd_project/Play/getProjectList').then(res => {
            wx.hideLoading()
            if (res.code === 200) {
                // 1. 数据分类
                const allProjects = {
                    experience: res.data.filter(item => item.play_type === 1),
                    challenge: res.data.filter(item => item.play_type === 2),
                    directReward: res.data.filter(item => item.play_type === 3),
                    exchange: res.data.filter(item => item.play_type === 4),
                    lead: res.data.filter(item => item.play_type === 5)
                };

                // 2. 添加类型文字
                const typeMap = { 1: "体验/打卡", 2: "闯关/完成项目", 3: "直接兑奖项目", 4: "兑换/核销奖品", 5: "留资项目" };
                Object.values(allProjects).forEach(projects => {
                    projects.forEach(item => item.play_type_text = typeMap[item.play_type]);
                });

                // 3. 缓存数据
                saveAllProjects('allPlayProjects', allProjects)
                    .then(() => resolve(res)) // 原始响应
                    .catch(() => reject(new Error('缓存失败')));
            } else {
                wx.showToast({ title: res.message, icon: 'none', duration: 3000 });
                reject(new Error('请求失败'));
            }
        })
    })
}
function getProjectList2() {
    wx.showLoading()
    return new Promise((resolve, reject) => {
        app.$http.get('/hd_project/Play/getProjectList?openid=1').then(res => {
            wx.hideLoading()
            if (res.code === 200) {
                resolve(res)
            } else {
                wx.showToast({ title: res.message, icon: 'none', duration: 3000 });
                reject(new Error('请求失败'));
            }
        })
    })
}

// 缓存工具函数
async function saveAllProjects(key, data) {
    try {
        await wx.setStorage({ key, data });
        return true;
    } catch (err) {
        return false;
    }
}

// 点位签到
function playGame(...args) {
    let pid, code, option;
    if (typeof args[0] === 'object') {
        pid = args[0].pid;
        code = args[0].code;
        option = args[0].option;
    } else {
        pid = args[0];
        code = args[1];
        option = args[2];
    }
    wx.showLoading()
    return new Promise((resolve, reject) => {
        let params = {
            pid: pid,
            code: code ? code : "",
            option: option ? option : "",
        }
        console.log(params)
        app.$http.post('/hd_project/Play/playGame', params, false, "json").then(res => {
            wx.hideLoading()
            if (res.code == 200) {
                resolve(res)
            } else {
                debugger
                wx.showModal({
                    // title: '失败',
                    content: res.message,
                    complete: (res) => {
                        if (res.cancel) {

                        }

                        if (res.confirm) {

                        }
                    }
                })

                console.log(res)
                  reject(res)
            }
        })
    })
}

// 抽奖
function luckDraw(...args) {
    let pid, gid, code, option;
    if (typeof args[0] === 'object') {
        pid = args[0].pid;
        gid = args[0].gid;
        code = args[0].code;
        option = args[0].option;
    } else {
        pid = args[0];
        gid = args[1];
        code = args[2];
        option = args[3];
    }
    return new Promise((resolve, reject) => {
        let params = {
            pid: pid,
            gid: gid ? gid : 0,
            option: option ? option : "",
            code: code ? code : "",
        }

        app.$http.post('/hd_project/Play/luckDraw', params).then(res => {
            if (res.code == 200) {
                resolve(res)
            } else {
                wx.showModal({
                    // title: '失败',
                    content: res.message,
                    complete: (res) => {
                        if (res.cancel) {

                        }

                        if (res.confirm) {

                        }
                    }
                })

                console.log(res)
                reject(res)
            }
        })
    })
}

// 核销
function getGift(...args) {
    let pid, gid;
    if (typeof args[0] === 'object') {
        pid = args[0].pid;
        gid = args[0].gid;
    } else {
        pid = args[0];
        gid = args[1];
    }
    wx.showLoading()
    return new Promise((resolve, reject) => {
        let params = {
            pid: pid,
            gid: gid ? gid : 0,
            code: 0,
        }
        app.$http.get('/hd_project/Play/getGift', params).then(res => {
            wx.hideLoading()
            if (res.code == 200) {
                resolve(res)
            } else {
                wx.showModal({
                    // title: '失败',
                    content: res.message,
                    complete: (res) => {
                        if (res.cancel) {

                        }

                        if (res.confirm) {

                        }
                    }
                })
                  reject(res)
            }
        })
    })
}

// 获取礼品列表
function getluckDrawStock(...args) {
    let pid;
    if (typeof args[0] === 'object') {
        pid = args[0].pid;
    } else {
        pid = args[0];
    }
    wx.showLoading()
    return new Promise((resolve, reject) => {
        let params = {
            pid: pid ? pid : 0,
        }
        app.$http.get('/hd_project/Play/getluckDrawStock', params).then(res => {
            wx.hideLoading()
            if (res.code == 200) {
                resolve(res)
            } else {
                wx.showToast({
                    title: res.message,
                    icon: "none",
                    duration: "3000"
                })

                console.log(res)
                  reject(res)
            }
        })
    })
}

// 上传图片
function uploadImg(...args) {
    let pid, img;
    if (typeof args[0] === 'object') {
        pid = args[0].pid;
        img = args[0].img;
    } else {
        pid = args[0];
        img = args[1];
    }
    return new Promise((resolve, reject) => {
        let params = {
            pid: pid,
            img: img ? img : "",
        }

        app.$http.post('/hd_project/Play/uploadImg', params, false, "json").then(res => {
            if (res.code == 200) {
                resolve(res)
            } else {
                wx.showModal({
                    // title: '失败',
                    content: res.message,
                    complete: (res) => {
                        if (res.cancel) {

                        }

                        if (res.confirm) {

                        }
                    }
                })

                console.log(res)
                  reject(res)
            }
        })
    })
}
// 上传图片
function getUserProjectInfo(...args) {
    let pid, img;
    if (typeof args[0] === 'object') {
        pid = args[0].pid;
    } else {
        pid = args[0];
    }
    return new Promise((resolve, reject) => {
        let params = {
            pid: pid,
        }

        app.$http.get('/hd_project/Play/getUserProjectInfo', params, false, "json").then(res => {
            if (res.code == 200) {
                resolve(res)
            } else {
                wx.showModal({
                    // title: '失败',
                    content: res.message,
                    complete: (res) => {
                        if (res.cancel) {

                        }

                        if (res.confirm) {

                        }
                    }
                })

                console.log(res)
            }
        })
    })
}

module.exports = {
    getProjectList2,  //测试用
    
    getProjectList,
    playGame,
    getluckDrawStock,
    luckDraw,
    getGift,
    getCarStore,
    uploadImg,
    getUserProjectInfo,
}